    <script setup>
    import { ref, onMounted } from 'vue'
    import { ElMessage, ElMessageBox } from 'element-plus'
    import { getUserList, addUser, updateUser, deleteUser } from '@/apis/user'
    
    const list = ref([])
    const total = ref(0)
    const page = ref(1)
    const pageSize = ref(8)
    const search = ref('')
    
    const fetchList = async () => {
      const res = await getUserList({ page: page.value, pageSize: pageSize.value, keyword: search.value })
      if (res.code === 1) {
        list.value = res.data.list
        total.value = res.data.total
      }
    }
    onMounted(fetchList)
    
    // 时间格式化
    function formatDate(val) {
      if (!val) return ''
      const d = new Date(val)
      return d.toLocaleString()
    }
    
    // 弹窗相关
    const dialogVisible = ref(false)
    const dialogType = ref('add') // add/edit
    const form = ref({ id: '', username: '', password: '', role: '' })
    const formRef = ref(null)
    const rules = {
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      role: [{ required: true, message: '请选择角色', trigger: 'change' }]
    }
    
    const onAdd = () => {
      dialogType.value = 'add'
      form.value = { id: '', username: '', password: '', role: '' }
      dialogVisible.value = true
    }
    const onEdit = (row) => {
      dialogType.value = 'edit'
      form.value = { id: row.id, username: row.username, password: '', role: row.role }
      dialogVisible.value = true
    }
    const onSubmit = () => {
      formRef.value.validate(async (valid) => {
        if (!valid) return
        if (dialogType.value === 'add') {
          const res = await addUser(form.value)
          if (res.code === 1) {
            ElMessage.success('新增成功')
            dialogVisible.value = false
            fetchList()
          }
        } else {
          const { id, username, role } = form.value
          const res = await updateUser({ id, username, role })
          if (res.code === 1) {
            ElMessage.success('编辑成功')
            dialogVisible.value = false
            fetchList()
          }
        }
      })
    }
    const onDelete = (row) => {
      ElMessageBox.confirm('确定要删除该用户吗？', '提示', { type: 'warning' })
        .then(async () => {
          const res = await deleteUser({ id: row.id })
          if (res.code === 1) {
            ElMessage.success('删除成功')
            fetchList()
          }
        })
    }
    </script>
    
<template>
  <div>
    <h2>用户管理</h2>
    <div class="user-header">
      <el-input v-model="search" placeholder="搜索用户名" style="width: 200px; margin-right: 12px;" @keyup.enter="fetchList" />
      <el-button type="primary" @click="onAdd">新增用户</el-button>
    </div>
    <el-table :data="list" style="width: 100%; margin-top: 16px;">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="role" label="角色" />
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template #default="scope">{{ formatDate(scope.row.createTime) }}</template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
        <template #default="scope">{{ formatDate(scope.row.updateTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="onEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageSize"
      :total="total"
      layout="total, prev, pager, next, jumper"
      style="margin-top: 16px; text-align: right;"
      @current-change="fetchList"
      @size-change="fetchList"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogType === 'add' ? '新增用户' : '编辑用户'">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item v-if="dialogType === 'add'" label="密码" prop="password">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin" />
            <el-option label="普通用户" value="user" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>


<style scoped>
.user-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

/* 新增：让主容器背景透明 */
:deep(.el-table),
:deep(.el-table__header-wrapper),
:deep(.el-table__body-wrapper),
:deep(.el-table th),
:deep(.el-table td),
:deep(.el-table__cell),
:deep(.el-table__row) {
  background: rgba(255, 255, 255, 0.2) !important;
}
:deep(.el-table) {
  background: rgba(255,255,255,0.2) !important;
  border-radius: 16px !important;
  overflow: hidden;
}

:deep(.el-table__header),
:deep(.el-table__header th),
:deep(.el-table thead),
:deep(.el-table thead tr),
:deep(.el-table thead th) {
  background: rgba(255, 255, 255, 0.2) !important;
}

:deep(.el-dialog),
:deep(.el-dialog__body) {
  background: rgb(255, 255, 255) !important;
  border-radius: 0 !important;
}

:deep(.el-pagination) {
  background: rgba(255, 255, 255, 0) !important;
  border-radius: 8px !important;
}

:deep(.el-pager li) {
  background: rgba(255, 255, 255, 0.5) !important;
  border-radius: 6px !important;
}
div:has(> .user-header) {
  background: transparent !important;
  box-shadow: none !important;
}
</style> 